package com.jctrip.hotel.source.xxl.wehotel.thread;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.model.wehotel.resq.GetHotelRoomStatus;
import com.jctrip.hotel.source.service.wehotel.WeHotelService;
import com.jctrip.hotel.source.service.wehotel.WeHotelTaskService;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author henry
 * @Date 2023/6/19 22:31
 * @Description
 */
@Slf4j
public class SyncWehotelRatesTaskThread implements Runnable {

    private final String appId;

    private final String key;

    private final Integer days;

    public SyncWehotelRatesTaskThread(String appId, String key, Integer days) {
        this.appId = appId;
        this.key = key;
        this.days = days;
    }

    @Override
    public void run() {
        WeHotelTaskService weHotelTaskService = BeanContext.getApplicationContext().getBean(WeHotelTaskService.class);
        WeHotelService weHotelService = BeanContext.getApplicationContext().getBean(WeHotelService.class);
        RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);

        while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_RATE_INNID_LIST) > 0) {
            try {
                String innId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_WEHOTEL_RATE_INNID_LIST);
                if (StringUtils.isBlank(innId)) {
                    continue;
                }
                String hotelId = weHotelService.parseHotelId(innId);
                GetHotelRoomStatus getHotelRoomStatus = new GetHotelRoomStatus();
                // 默认15天的数据
                getHotelRoomStatus.setDays(days);
                String nowDate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
                getHotelRoomStatus.setEndOfDay(nowDate);
                getHotelRoomStatus.setInnId(hotelId);
                weHotelTaskService.syncWehotelRatesTaskPop(appId, key, getHotelRoomStatus);
            } catch (Exception e) {
                log.error("SyncWehotelRatesTaskThread error", e);
            }
        }
    }
}
